<script setup>
import { ref, reactive } from 'vue'
import { useUserStore } from '@/store/user'
import PageContainer from '@/components/PageContainer.vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const userForm = reactive({
  ...userStore.userInfo,
  password: ''
})

const loading = ref(false)

const handleSubmit = async () => {
  try {
    loading.value = true
    await request.put(`/api/users/${userForm.id}`, userForm)
    ElMessage.success('保存成功')
    userStore.setUserInfo(userForm)
  } catch (error) {
    ElMessage.error(error.message || '保存失败')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <page-container title="个人信息">
    <el-form :model="userForm" label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="userForm.username" disabled />
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="userForm.nickname" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="userForm.phone" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="userForm.email" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="userForm.password" type="password" placeholder="不修改请留空" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="handleSubmit">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </page-container>
</template> 